package cn.ekuma.epos.analysis.businesspartnerCategory;

import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JScrollPane;
import javax.swing.JToolBar;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.SwingConstants;

import com.openbravo.pos.base.*;
import org.jdesktop.swingx.JXTreeTable;
import cn.ekuma.epos.datalogic.I_DataLogicERP;
import cn.ekuma.epos.module.ERP_UI_Swing_Model;
import cn.ekuma.rabbit.spi.I_ModelPart;

import com.openbravo.bean.erp.OperationProductCategory;
import com.openbravo.beans.JCalendarDialog;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.gui.MessageInf;
import com.openbravo.data.loader.query.QBFCompareEnum;
import com.openbravo.data.user.EditorCreator;
import com.openbravo.format.Formats;

import com.openbravo.pos.forms.JPanelView;
import com.openbravo.pos.panels.QuickTimerFilter;
import com.openbravo.pos.panels.event.TimeCycleChangedEvent;
import com.openbravo.pos.panels.event.TimeCycleChangedListener;
import com.openbravo.pos.panels.navigator.JOperationProductCategoryNavigator;
import com.openbravo.pos.panels.navigator.event.OperationProductCategoryChangedListener;
import java.awt.CardLayout;

public class JCustomerCategoryAnalysisPanel extends JPanel implements TimeCycleChangedListener,EditorCreator,JPanelView, BeanFactoryApp,I_ModelPart<ERP_UI_Swing_Model> {
	private JTextField jTxtStartDate;
	private JTextField jTxtEndDate;
	private QuickTimerFilter timerFilter;
	private JButton jExecFilter;
	private JButton btnNewButton_1;
	private JPanel jNavigator;
	private JScrollPane jScrollPane2;
	private JXTreeTable jXTreeTable2;
	AnalysisModelManager manager;
	private AppView app;
	private I_DataLogicERP dlSales;
	private Object[] beforeFilterParas;
	private JOperationProductCategoryNavigator operationProductCategoryNavigator;
	private CardLayout cl1;
	/**
	 * Create the panel.
	 */
	public JCustomerCategoryAnalysisPanel() {
		initComponents();
		jXTreeTable2.setAutoResizeMode(JXTreeTable.AUTO_RESIZE_OFF);
        jXTreeTable2.setColumnControlVisible(true);
	}
	private void initComponents() {
		setLayout(null);
		setLayout(new BorderLayout(0, 0));
		
		JPanel panel = new JPanel();
		add(panel, BorderLayout.NORTH);
		panel.setLayout(new BorderLayout(0, 0));
		
		JToolBar toolBar = new JToolBar();
		panel.add(toolBar, BorderLayout.NORTH);
		toolBar.setRollover(true);
		
		JLabel label = new JLabel();
		label.setText(AppLocal.getIntString("Label.StartDate"));
		toolBar.add(label);
		
		jTxtStartDate = new JTextField();
		jTxtStartDate.setPreferredSize(new Dimension(150, 22));
		jTxtStartDate.setMinimumSize(new Dimension(6, 15));
		toolBar.add(jTxtStartDate);
		
		JButton btnDateStart = new JButton();
		btnDateStart.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/openbravo/images/date.png"))); // NOI18N
		btnDateStart.setVerticalTextPosition(SwingConstants.BOTTOM);
		btnDateStart.setPreferredSize(new Dimension(55, 25));
		btnDateStart.setHorizontalTextPosition(SwingConstants.CENTER);
		btnDateStart.setFocusable(false);
		btnDateStart.addActionListener(new java.awt.event.ActionListener() {
             public void actionPerformed(java.awt.event.ActionEvent evt) {
                 btnDateStartActionPerformed(evt);
             }
         });
		toolBar.add(btnDateStart);
		
		JLabel label_1 = new JLabel();
		label_1.setText(AppLocal.getIntString("Label.EndDate"));
		toolBar.add(label_1);
		
		jTxtEndDate = new JTextField();
		jTxtEndDate.setPreferredSize(new Dimension(150, 22));
		toolBar.add(jTxtEndDate);
		
		JButton btnDateEnd = new JButton();
		btnDateEnd.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/openbravo/images/date.png"))); // NOI18N
		btnDateEnd.setFocusable(false);
		btnDateEnd.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
		btnDateEnd.setPreferredSize(new java.awt.Dimension(55, 25));
		btnDateEnd.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
		btnDateEnd.addActionListener(new java.awt.event.ActionListener() {
		    public void actionPerformed(java.awt.event.ActionEvent evt) {
		        btnDateEndActionPerformed(evt);
		    }
		});
		toolBar.add(btnDateEnd);
		
		jExecFilter = new JButton("");
		jExecFilter.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/openbravo/images/1rightarrow.png"))); // NOI18N
		jExecFilter.setFocusable(false);
		jExecFilter.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
		jExecFilter.setPreferredSize(new java.awt.Dimension(55, 25));
		jExecFilter.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
		jExecFilter.addActionListener(new java.awt.event.ActionListener() {
		    public void actionPerformed(java.awt.event.ActionEvent evt) {
		        jButton5ActionPerformed(evt);
		    }
		});
		toolBar.add(jExecFilter);
		
		btnNewButton_1 = new JButton("");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				m_jResert1ActionPerformed(e);
			}
		});
		btnNewButton_1.setIcon(new ImageIcon(JCustomerCategoryAnalysisPanel.class.getResource("/com/openbravo/images/reload.png")));
		toolBar.add(btnNewButton_1);
		
		timerFilter=new QuickTimerFilter();
        panel.add(timerFilter,BorderLayout.CENTER);
        timerFilter.addTimeCycleChangedListener(this);
		
		JPanel jNavigatorToolBar = new JPanel();
		panel.add(jNavigatorToolBar, BorderLayout.EAST);
		
		JButton button_4 = new JButton(new ImageIcon(JCustomerCategoryAnalysisPanel.class.getResource("/com/openbravo/images/search.png")));
		button_4.setPreferredSize(new Dimension(33, 23));
		button_4.addActionListener(new ActionListener() {
        	public void actionPerformed(ActionEvent e) {
        		jNavigator.setVisible(!jNavigator.isVisible());
        	}
        });
		jNavigatorToolBar.add(button_4);
		
		JButton button_5 = new JButton(AppLocal.getIntString("label.operationProductCategory"));
		button_5.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				cl1.show(jNavigator, "operationCategory");
			}
		});
		jNavigatorToolBar.add(button_5);
		
		JPanel panel_1 = new JPanel();
		add(panel_1, BorderLayout.CENTER);
		panel_1.setLayout(new BorderLayout(0, 0));
		
		 jScrollPane2 = new javax.swing.JScrollPane();
		 panel_1.add(jScrollPane2, BorderLayout.CENTER);
	        jXTreeTable2 = new org.jdesktop.swingx.JXTreeTable();
	        
	        	        jScrollPane2.setViewportView(jXTreeTable2);
		
		jNavigator = new JPanel();
		jNavigator.setVisible(false);
		jNavigator.setPreferredSize(new Dimension(240, 10));
		add(jNavigator, BorderLayout.EAST);
		jNavigator.setLayout(new CardLayout(0, 0));
	}
	
	 public void expandALL(){
         jXTreeTable2.expandAll();
         //SwingUtil.PreferredJXTreeTableColumn(jXTreeTable2);
    }
	
	protected void m_jResert1ActionPerformed(ActionEvent e) {
		beforeFilterParas=null;
		jTxtStartDate.setText(null);
		jTxtEndDate.setText(null);
		timerFilter.resert();
	}
	protected void jButton5ActionPerformed(ActionEvent evt) {
		reQuery();
	}
	
	
	 private void btnDateStartActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDateStartActionPerformed
	        Date date;
	        try {
	            date = (Date) Formats.TIMESTAMP.parseValue(jTxtStartDate.getText());
	        } catch (BasicException e) {
	            date = null;
	        }
	        date = JCalendarDialog.showCalendarTimeHours(this, date);
	        if (date != null) {
	            jTxtStartDate.setText(Formats.TIMESTAMP.formatValue(date));
	        }
	}//GEN-LAST:event_btnDateStartActionPerformed

	    private void btnDateEndActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDateEndActionPerformed
	        Date date;
	        try {
	            date = (Date) Formats.TIMESTAMP.parseValue(jTxtEndDate.getText());
	        } catch (BasicException e) {
	            date = null;
	        }
	        date = JCalendarDialog.showCalendarTimeHours(this, date);
	        if (date != null) {
	            jTxtEndDate.setText(Formats.TIMESTAMP.formatValue(date));
	        }
	}//GEN-LAST:event_btnDateEndActionPerformed

	    
	@Override
	public String getTitle() {
		return null;
	}
	@Override
	public void activate() throws BasicException {
        operationProductCategoryNavigator.loadData();
		
	}
	@Override
	public boolean deactivate() {
		return true;
	}
	@Override
	public JComponent getComponent() {
		return this;
	}
	
	@Override
	public Object createValue() throws BasicException {
		Object[] afilter = new Object[6];
        // Date range
      Object startdate = Formats.TIMESTAMP.parseValue(jTxtStartDate.getText());
      Object enddate = Formats.TIMESTAMP.parseValue(jTxtEndDate.getText());

      afilter[0] = (startdate == null) ? QBFCompareEnum.COMP_NONE : QBFCompareEnum.COMP_GREATEROREQUALS;
      afilter[1] = startdate;
      afilter[2] = (enddate == null) ? QBFCompareEnum.COMP_NONE : QBFCompareEnum.COMP_LESS;
      afilter[3] = enddate;
          
     //Operation Cateory
      afilter[4]=QBFCompareEnum.COMP_NONE;
      afilter[5]=null;
    
          
     return afilter;
	}
	
	private void reQuery() {
		if(beforeFilterParas==null)
			try {
				beforeFilterParas=(Object[]) createValue();
			} catch (BasicException e) {
				new MessageInf(e).show(JCustomerCategoryAnalysisPanel.this);
				return;
		}
		try {
			
			manager.query(beforeFilterParas);
			jXTreeTable2.setTreeTableModel(manager.getTreeTableModel());
			expandALL();
			if (beforeFilterParas[1] != null){
				jTxtStartDate.setText(Formats.TIMESTAMP
						.formatValue(beforeFilterParas[1]));
			}else{
				jTxtStartDate.setText(null);
			}if (beforeFilterParas[3] != null){
				jTxtEndDate.setText(Formats.TIMESTAMP
						.formatValue(beforeFilterParas[3]));
			}else{
				jTxtEndDate.setText(null);
			}
		} catch (BasicException e) {
			new MessageInf(e).show(this);
		}
	}
	
	
	@Override
	public Object getBean() {	
		return this;
	}
	@Override
	public void init(I_AppView app) throws BeanFactoryException {
		this.app=(AppView) app;
        manager=new AnalysisModelManager(dlSales);
		jXTreeTable2.setTreeTableModel(manager.getTreeTableModel());
		
		cl1=(CardLayout)(jNavigator.getLayout());
		operationProductCategoryNavigator =new JOperationProductCategoryNavigator(dlSales);
        operationProductCategoryNavigator.addOperationProductCategoryChangedListener(new OperationProductCategoryChangedListener(){

			@Override
			public void valueSelectChanged(OperationProductCategory obj) {
				 if(beforeFilterParas==null)
						try {
							beforeFilterParas=(Object[]) createValue();
						} catch (BasicException e) {
							new MessageInf(e).show(JCustomerCategoryAnalysisPanel.this);
							return;
						}
						//Operation Cateory
						beforeFilterParas[4]=QBFCompareEnum.COMP_EQUALS;
				        beforeFilterParas[5]=obj.getID(); 
						reQuery(); 
				
			}
        	
        });
    	jNavigator.add(operationProductCategoryNavigator,"operationCategory");
	}
	
	@Override
	public void timeCycleChanged(TimeCycleChangedEvent e) {
		if(beforeFilterParas==null)
			try {
				beforeFilterParas=(Object[]) createValue();
			} catch (BasicException ex) {
				new MessageInf(ex).show(this);
				return;
			}
			
			 // Date range
		      Object startdate = e.getStartDate();
		      Object enddate = e.getEndDate();

		      beforeFilterParas[0] = (startdate == null) ? QBFCompareEnum.COMP_NONE : QBFCompareEnum.COMP_GREATEROREQUALS;
		      beforeFilterParas[1] = startdate;
		      beforeFilterParas[2] = (enddate == null) ? QBFCompareEnum.COMP_NONE : QBFCompareEnum.COMP_LESS;
		      beforeFilterParas[3] = enddate;	
		      reQuery();
	}
	@Override
	public String getModelName() {
		return ERP_UI_Swing_Model.MODEL_NAME;
	}
	@Override
	public void setModel(ERP_UI_Swing_Model mode) {
		dlSales=mode.getDLERP();
		
	}

}
